package com.cash.loan.modular.cash.controller;

import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.ChannelService;
import com.cash.loan.modular.cash.transfer.ChannelDto;
import com.cash.loan.modular.cash.transfer.ChannelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Date 2018-08-01 9:41
 */
@Api(description = "渠道管理")
@Controller
@RequestMapping(value = "/cash/place")
public class PlaceController {

    private static final String PREFIX = "/cash/place";

    @Autowired
    private ChannelService channelService;

    @RequestMapping(value = "/listPage")
    public String toListPage() {
        return PREFIX + "/place_list.html";
    }

    @RequestMapping(value = "/addPage")
    public String addPage() {
        return PREFIX + "/place_add.html";
    }

    @RequestMapping(value = "/versionPage")
    public String versionPage() {
        return PREFIX + "/place_version.html";
    }

    @RequestMapping(value = "/editPage")
    public String editPage() {
        return PREFIX + "/place_edit.html";
    }

    @ApiOperation("渠道列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse getChannelList(@RequestParam(value = "keyword",required = false) String keyword) {
        List<ChannelVo> channelList = channelService.channelList(keyword);
        return ServerResponse.getSuccess("success",channelList);
    }

    @ApiOperation("检查市场名是否存在")
    @PostMapping(value = "/checkName")
    @ResponseBody
    public ServerResponse checkChannelName(@RequestParam(value = "channelName") String channelName) {
        Boolean exist = channelService.checkChannelNameExist(channelName);
        Map<String,Boolean> result = new HashMap<>(1);
        result.put("exist",exist);
        return ServerResponse.getSuccess("success",result);
    }

    @ApiOperation("检查渠道号是否存在")
    @PostMapping(value = "/checkCode")
    @ResponseBody
    public ServerResponse checkChannelCode(@RequestParam(value = "channelCode") String channelCode) {
        Boolean exist = channelService.checkChannelCodeExist(channelCode);
        Map<String,Boolean> result = new HashMap<>(1);
        result.put("exist",exist);
        return ServerResponse.getSuccess("success",result);
    }

    @ApiOperation("保存渠道信息")
    @PostMapping(value = "/create")
    @ResponseBody
    public ServerResponse saveChannelInfo(@RequestBody ChannelDto channelDto,
                                          BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }

        Boolean saveRet = channelService.saveChannelInfo(channelDto);

        if(!saveRet) {
            return ServerResponse.getErrorMessage("保存失败");
        }
        return ServerResponse.getSuccess("保存成功");
    }

    @ApiOperation("改变送审状态")
    @PostMapping(value = "/changeType")
    @ResponseBody
    public ServerResponse changeUiType(Long id,Long uiType) {
        Boolean result = channelService.changeUiType(id,uiType);
        if(!result) {
            return ServerResponse.getErrorMessage("更改失败");
        }
        return ServerResponse.getSuccess("更改成功");
    }

    @ApiOperation("删除数据")
    @PostMapping(value = "/delete")
    @ResponseBody
    public ServerResponse deleteById(Long id) {
        Boolean result = channelService.deleteById(id);
        if(!result) {
            return ServerResponse.getErrorMessage("删除失败");
        }
        return ServerResponse.getSuccess("删除成功");
    }

    @ApiOperation("详情")
    @GetMapping(value = "/detail")
    @ResponseBody
    public ServerResponse detailById(Long id) {
        ChannelVo channelVo = channelService.getDetailById(id);
        return ServerResponse.getSuccess("success",channelVo);
    }

    @ApiOperation("更新数据")
    @PostMapping(value = "/update")
    @ResponseBody
    public ServerResponse updateChannel(@RequestBody ChannelDto channelDto,BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }
        Boolean updateRet = channelService.updateChannel(channelDto);
        if(!updateRet) {
            return ServerResponse.getErrorMessage("更新失败");
        }
        return ServerResponse.getSuccess("更新成功");
    }
}
